如果我們有個web專案會用到jquery
我們希望它是一個獨立的repo
可以透過subtree或是submodule來完成
這次介紹的是subtree
原本我們有個主專案mainRep
裡面有一個index.js
以及js/plug-in/jquery.js
現在我們希望jquery是利用subtree去管理
$ git rm -r js/plug-in/
rm 'js/plug-in/jquery.js'
$ git commit -m "rm js/plug-in/"
[master 081239f] rm js/plug-in/
1 file changed, 8755 deletions(-)
delete mode 100644 js/plug-in/jquery.js
$ git push
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 225 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To /Users/morrishsu/Documents/git/serverRep/
cddfd02..081239f master -> master
$ git remote add jqueryRep /Users/morrishsu/Documents/git/jqueryRep
$ git subtree add -P js/plug-in jqueryRep master
git fetch jqueryRep master
warning: no common commits
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /Users/morrishsu/Documents/git/jqueryRep
* branch master -> FETCH_HEAD
* [new branch] master -> jqueryRep/master
Added dir 'js/plug-in'
這時候查看主專案的資料夾內就有剛剛從subtree中取出來的jquery套件了
之後若是subtree的專案有異動
若在主專案內異動了subtree內的檔案,可以push到subtree的分支內
$ git subtree push -P js/plug-in jqueryRep master
若是我們要知道套件更新了,可以pull新的下來
$ git subtree pull -P js/plug-in jqueryRep master
不過別人從serverRep上clone下來的時候,雖然有包含subtree管理的資料
但是卻沒有subtree的遠端分支設定在clone下來的專案內